package com.example.administrator.u17.activity;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.u17.R;
import com.example.administrator.u17.adapter.DingYueAdapter;
import com.example.administrator.u17.modle.DingYueBean;
import com.example.administrator.u17.modle.MyDbBean;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import org.xutils.DbManager;
import org.xutils.common.Callback;
import org.xutils.ex.DbException;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class SearchClasifyActivity extends AppCompatActivity {

    private DingYueAdapter mAdapter;
    private ArrayList<DingYueBean.Comics> mData;
    private DbManager mDbManager;
    private PullToRefreshListView mListView;
    private ImageView mImageViewBack;
    private String mPath;
    private int mCurrentPage=1,mCurrentArgCon=2;
    private PopupWindow mPopWindow;
    private View mLayout;
    private TextView mPopWinTv;
    private TextView tvClick;
    private TextView tvUpdate;
    private TextView tvSave;
    private LinearLayout mLinearLayout;
    private Intent intent;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_search_clasify);
        mDbManager=x.getDb(new DbManager.DaoConfig());
        initView();
        initData();
        setData();
        setListener();
    }

    private void setListener() {
        mListView.setMode(PullToRefreshBase.Mode.BOTH);
        mListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                mCurrentPage=1;
                loadData();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                mCurrentPage++;
                loadData();
            }
        });

        mImageViewBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


        mPopWinTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(SearchClasifyActivity.this, "hahahahah", Toast.LENGTH_SHORT).show();
                //创建弹出窗口对象，指定内部的布局和宽高
                mPopWindow=new PopupWindow(mLayout, 300, ViewGroup.LayoutParams.WRAP_CONTENT);

                //设置窗口的点击事件
                //注意此处是坑，想实现点击外部隐藏弹出窗口，必须设置背景这样才会拥有外层容器类
                mPopWindow.setBackgroundDrawable(new ColorDrawable());
                mPopWindow.setOutsideTouchable(true);//设置点击外部的时候，窗口也可以消失
                //4、显示窗口
                mPopWindow.showAtLocation(mLinearLayout, Gravity.RIGHT|Gravity.TOP, 10, 0);
//                mPopWindow.showAtLocation();
            }
        });


        tvClick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mData.clear();
                mCurrentArgCon=1;
                loadData();
                mPopWinTv.setText(tvClick.getText());
                mPopWindow.dismiss();
            }
        });
        tvUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mData.clear();
                mCurrentArgCon=2;
                loadData();
                mPopWinTv.setText(tvUpdate.getText());
                mPopWindow.dismiss();
            }
        });
        tvSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mData.clear();
               mCurrentArgCon=3;
                loadData();
                mPopWinTv.setText(tvSave.getText());
                mPopWindow.dismiss();
            }
        });

        //PullToRefreshListView的点击事件
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(SearchClasifyActivity.this,ZhangJieActivity.class);
                intent.putExtra("id",mData.get(position).comicId);
                startActivity(intent);
            }
        });



    }

    private void setData() {
        mListView.setAdapter(mAdapter);
    }

    private void initData() {
        mData = new ArrayList<>();
        mAdapter = new DingYueAdapter(this,mData);
        loadData();



    }
    private void loadData(){
        intent = getIntent();
        mPath = intent.getStringExtra("clasifyPath")+mCurrentArgCon+"&page="+mCurrentPage;
        RequestParams params=new RequestParams(mPath);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                MyDbBean myDb=new MyDbBean();
                myDb.json=result;
                myDb.path=mPath;
                Calendar calendar=Calendar.getInstance();
                SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
                myDb.time=format.format(calendar.getTime());
                try {
                    mDbManager.save(myDb);
                } catch (DbException e) {
                    e.printStackTrace();
                }

//                Toast.makeText(SearchClasifyActivity.this, result, Toast.LENGTH_SHORT).show();
                DingYueBean bean=new Gson().fromJson(result,DingYueBean.class);
                mData.addAll(bean.data.returnData.comics);
                mAdapter.notifyDataSetChanged();
                mListView.onRefreshComplete();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

                try {
                    List<MyDbBean> data=mDbManager.selector(MyDbBean.class).where("path","=",mPath).findAll();

                    DingYueBean bean=new Gson().fromJson(data.get(0).json,DingYueBean.class);
                    mData.addAll(bean.data.returnData.comics);
                    mAdapter.notifyDataSetChanged();
                    mListView.onRefreshComplete();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                mListView.onRefreshComplete();
            }
        });
    }

    private void initView() {
        mListView= (PullToRefreshListView) findViewById(R.id.pl_search_clasify_avtivity);
        mImageViewBack= (ImageView) findViewById(R.id.iv_back_search_clasify_activity);

        mPopWinTv= (TextView) findViewById(R.id.tv_pop_search_clasify_activity);
        mLinearLayout= (LinearLayout) findViewById(R.id.activity_search_clasify);
//        //创建弹出窗口对象，指定内部的布局和宽高
//        mPopWindow=new PopupWindow(RecyclerView.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

        //转换布局文件
        mLayout=View.inflate(SearchClasifyActivity.this,R.layout.popwindow_layput,null);
        tvClick = (TextView) mLayout.findViewById(R.id.tv_click_popwin);
        tvUpdate = (TextView) mLayout.findViewById(R.id.tv_update_popwin);
        tvSave = (TextView) mLayout.findViewById(R.id.tv_save_popwin);

    }
}
